/************************* (C) COPYRIGHT 2008 Domain **************************
 * File Name   : modem.h                                                      *
 * Author      : Domain                                                       *
 * E-mail      : do.main@msn.com                                              *
 * Version     : 1.0                                                          *
 * Date        : 2008/5/27                                                    *
 * Description :                                                              *
 ******************************************************************************/

#ifndef __MODEM_H
#define __MODEM_H

#include "stm32f10x_type.h"
#include "usart.h"

typedef enum
{
  MODEM_OK, MODEM_FAIL
} MODEM_ERR;

typedef struct
{
    u8* command;  /* the AT Command we send to Modem */
    u8* result;   /* the result we expected */
    u16 timeout;  /* MAX time that we could wait */
} AT_CMD;

#define COMMAND_MODE    0
#define DATA_MODE       1
extern u8 currentMode;
extern u8* atResult;
extern u16 atResultLength;
extern s8 currentChannel;

extern int Send2Channel(s8 channel, const u8* buf, const u16 len);

#define SendAT(level, channel, ...)                                            \
        do {                                                                   \
          DebugPrintf(level, "Send Command: ");                                \
          DebugPrintf(level, __VA_ARGS__);                                     \
          DebugPrintf(level, "\r\n");                                          \
          if (channel < 0)                                                     \
            fprintf(F_MODEM, __VA_ARGS__);                                     \
          else                                                                 \
            Send2ChannelBuffer(channel, __VA_ARGS__);                          \
        } while(0)

#define waitResult(c, e, t, begin, end) wait_result((c), (e), (t), (begin), (end), 0)
#define WaitResult(c, e, t)             wait_result((c), (e), (t), "\r\n", "\r\n", 0)

bool wait_result(s8 channel, u8* expected, u16 timeout, u8* begin, u8* end, u16 length);
bool ActiveModem(void);
MODEM_ERR ActiveGPRS(void);

void Send2ChannelBuffer(s8 port, const char *format, ...);

#endif /* __MODEM_H */

/************************* (C) COPYRIGHT 2008 Domain ******* END OF FILE ******/
